package org.amphiprion.montrictracv4;

import java.io.Serializable;

import org.amphiprion.apiforanywebsite.engine.WebMarshaller;
import org.amphiprion.montrictracv4.db.model.Ludotheque;
import org.amphiprion.montrictracv4.db.model.Search;
import org.amphiprion.montrictracv4.fragment.GameListFragment;
import org.amphiprion.montrictracv4.fragment.LudothequeFragment;
import org.amphiprion.montrictracv4.fragment.MenuFragment;
import org.amphiprion.montrictracv4.fragment.SearchEditFragment;
import org.amphiprion.montrictracv4.fragment.SearchListFragment;
import org.amphiprion.montrictracv4.fragment.TricTracFragment;
import org.amphiprion.montrictracv4.web.model.WebGameDetail;
import org.amphiprion.montrictracv4.web.model.WebUserCarac;
import org.amphiprion.montrictracv4.web.model.WebUserInfo;
import org.amphiprion.montrictracv4.web.model.WebUserMecanism;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.FrameLayout;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity implements Serializable {
	private MenuFragment mMenuFragment;
	public static MainActivity sThis;
	private TricTracFragment mainFragment;
	private Bundle bundle = new Bundle();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sThis = this;
		setContentView(R.layout.activity_main);
		setTitle(getResources().getString(R.string.ludoteque));
		// Action bar
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_support_bar));
		// sliding menu
		setSlidingActionBarEnabled(false);
		getSlidingMenu().setBehindOffsetRes(R.dimen.actionbar_home_width);
		getSlidingMenu().setBehindScrollScale(0.5F);
		getSlidingMenu().setTouchModeAbove(0); // TODO 2 pour tablette

		// menu fragment

		FrameLayout frameLayout = new FrameLayout(this);
		frameLayout.setId(R.id.menuRoot);
		setBehindContentView(frameLayout);
		FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
		mainFragment = new LudothequeFragment();
		localFragmentTransaction.add(R.id.fragmentContainer, (SherlockFragment) mainFragment);
		this.mMenuFragment = new MenuFragment();
		localFragmentTransaction.add(R.id.menuRoot, this.mMenuFragment);
		localFragmentTransaction.commit();

		// debug();
	}

	public void showGameList(Ludotheque ludoteque) {
		App.currentLudotheque = ludoteque;
		FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
		mainFragment = new GameListFragment();
		localFragmentTransaction.replace(R.id.fragmentContainer, (SherlockFragment) mainFragment);
		localFragmentTransaction.addToBackStack(null);
		localFragmentTransaction.commit();
		supportInvalidateOptionsMenu();
	}

	public void showLudothequeList() {
		App.currentLudotheque = null;
		App.currentSearch = null;
		setTitle(getResources().getString(R.string.ludoteque));
		FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
		mainFragment = new LudothequeFragment();
		localFragmentTransaction.replace(R.id.fragmentContainer, (SherlockFragment) mainFragment);
		localFragmentTransaction.addToBackStack(null);
		localFragmentTransaction.commit();
		supportInvalidateOptionsMenu();
	}

	@Override
	public void onBackPressed() {
		if (mainFragment instanceof GameListFragment) {
			if (App.currentSearch == null) {
				showLudothequeList();
			} else {
				showSearchList();
			}
		} else if (mainFragment instanceof SearchEditFragment) {
			showSearchList();
		} else {
			this.finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getSupportMenuInflater();
		int menuId = R.menu.ludotheque_list_menu;
		if (mainFragment instanceof GameListFragment) {
			menuId = R.menu.game_list_menu;
		} else if (mainFragment instanceof SearchListFragment) {
			menuId = R.menu.search_list_menu;
		} else if (mainFragment instanceof SearchEditFragment) {
			menuId = R.menu.search_edit_menu;
		}
		inflater.inflate(menuId, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			break;
		case R.id.action_settings:
			startActivity(new Intent(this, SettingActivity.class));
			break;
		default:
			mainFragment.menuOptionClicked(item.getItemId());
			break;
		}
		return true;
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// MenuInflater inflater = getSupportMenuInflater();
	// inflater.inflate(R.menu.main, menu);
	// return true;
	// }

	public void debug() {
		// Inflate the menu; this adds items to the action bar if it is present.
		new Thread() {
			@Override
			public void run() {
				try {
					String url = "http://www.trictrac.net/jeu-de-societe/le-seigneur-des-anneaux-voyage-a-rhosgobel/infos";
					Document doc = Jsoup.connect(url).get();
					WebMarshaller<WebGameDetail> m = new WebMarshaller<WebGameDetail>(WebGameDetail.class);
					WebGameDetail wgd = m.marshall(doc).get(0);

					Log.d(ApplicationConstants.PACKAGE, "********** name:" + wgd.name);
					Log.d(ApplicationConstants.PACKAGE, "********** image:" + wgd.image);
					Log.d(ApplicationConstants.PACKAGE, "********** ags:" + wgd.ages);
					Log.d(ApplicationConstants.PACKAGE, "********** extension of:" + wgd.extensionOf);
					for (WebUserInfo u : wgd.userInfos) {
						Log.d(ApplicationConstants.PACKAGE, "********** userInfos:" + u);
					}
					for (WebUserCarac u : wgd.userCaracs) {
						Log.d(ApplicationConstants.PACKAGE, "********** userCarac:" + u);
					}
					for (WebUserMecanism u : wgd.userMecanisms) {
						Log.d(ApplicationConstants.PACKAGE, "********** userMeca:" + u);
					}
				} catch (Exception e) {
					Log.e("TAG", "", e);
				}

			};
		}.start();
	}

	public void refresh() {
		if (mainFragment != null) {
			mainFragment.refresh();
		}

	}

	public void showSearch(Search search) {
		App.currentLudotheque = null;
		App.currentSearch = search;
		if (search == null) {
			setTitle(getResources().getString(R.string.search_new));
		} else {
			setTitle(search.name);
		}
		FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
		mainFragment = new SearchEditFragment();
		localFragmentTransaction.replace(R.id.fragmentContainer, (SherlockFragment) mainFragment);
		localFragmentTransaction.addToBackStack(null);
		localFragmentTransaction.commit();
		supportInvalidateOptionsMenu();

	}

	public void showSearchList() {
		App.currentLudotheque = null;
		App.currentSearch = null;
		setTitle(getResources().getString(R.string.search_list));
		FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
		mainFragment = new SearchListFragment();
		localFragmentTransaction.replace(R.id.fragmentContainer, (SherlockFragment) mainFragment);
		localFragmentTransaction.addToBackStack(null);
		localFragmentTransaction.commit();
		supportInvalidateOptionsMenu();
	}

}
